package com.usian.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbContentCategoryMapper;
import com.usian.mapper.TbContentMapper;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.pojo.TbContentCategoryExample;
import com.usian.pojo.TbContentExample;
import com.usian.redis.RedisClient;
import com.usian.utils.AdNode;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TbConServiceImpl implements TbConService {
    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    @Autowired
    private TbContentMapper tbContentMapper;
    @Autowired
    private RedisClient redisClient;

    @Value("${AD_CATEGORY_ID}")
    private Long AD_CATEGORY_ID;

    @Value("${AD_HEIGHT}")
    private Integer AD_HEIGHT;

    @Value("${AD_WIDTH}")
    private Integer AD_WIDTH;

    @Value("${AD_HEIGHTB}")
    private Integer AD_HEIGHTB;

    @Value("${AD_WIDTHB}")
    private Integer AD_WIDTHB;

    @Override
    public List<TbContentCategory> selectContentCategoryByParentId(Integer id) {
        TbContentCategoryExample t=new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = t.createCriteria();
        criteria.andStatusEqualTo(1);
        criteria.andParentIdEqualTo((long)id);
        List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(t);
        return list;
    }

    @Override
    public int insertContentCategory(Integer parentId, String name) {

        TbContentCategory tb=new TbContentCategory();
        Date date=new Date();
        tb.setParentId((long)parentId);
        tb.setCreated(date);
        tb.setUpdated(date);
        tb.setStatus(1);
        tb.setSortOrder(1);
        tb.setName(name);
        tb.setIsParent(false);

        int i = tbContentCategoryMapper.insertSelective(tb);
        TbContentCategory t=new TbContentCategory();
        t.setId((long)parentId);
        t.setIsParent(true);

        int i1 = tbContentCategoryMapper.updateByPrimaryKeySelective(t);
        return i+i1;
    }

    @Override
    public void deleteContentCategoryById(long categoryId) {
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(categoryId);
        if (tbContentCategory.getIsParent()==false){
            //修改状态
            TbContentCategory t=new TbContentCategory();
            t.setId(categoryId);
            t.setStatus(2);
             tbContentCategoryMapper.updateByPrimaryKeySelective(t);
             //修改父类是否为父类的状态
            TbContentCategory t1=new TbContentCategory();
           t1.setId(tbContentCategory.getParentId());
           t1.setIsParent(false);
           tbContentCategoryMapper.updateByPrimaryKeySelective(t1);
           //删除广告
           TbContentExample tbContentExample=new TbContentExample();
            TbContentExample.Criteria criteria = tbContentExample.createCriteria();
            criteria.andCategoryIdEqualTo(categoryId);
            tbContentMapper.deleteByExample(tbContentExample);

        }else {
            //删除广告
            TbContentExample tbContentExample=new TbContentExample();
            TbContentExample.Criteria criteria1 = tbContentExample.createCriteria();
            criteria1.andCategoryIdEqualTo(categoryId);
            tbContentMapper.deleteByExample(tbContentExample);
            //修改父类是否为父类的状态
            TbContentCategory t1=new TbContentCategory();
            t1.setId(tbContentCategory.getParentId());
            t1.setIsParent(false);
            tbContentCategoryMapper.updateByPrimaryKeySelective(t1);

            //修改状态和改成子类
            TbContentCategory t=new TbContentCategory();
            t.setId(categoryId);
            t.setStatus(2);
            t.setIsParent(false);
            tbContentCategoryMapper.updateByPrimaryKeySelective(t);

            //把子类查出来继续递归
            TbContentCategoryExample e=new TbContentCategoryExample();
            TbContentCategoryExample.Criteria criteria = e.createCriteria();
            criteria.andParentIdEqualTo(categoryId);
            List<TbContentCategory> list = tbContentCategoryMapper.selectByExample(e);

            deleteContentCategoryById(list.get(0).getId());

        }




    }

    @Override
    public int updateContentCategory(Integer id, String name) {
        TbContentCategory t=new TbContentCategory();
        t.setId((long)id);
        t.setName(name);
        return tbContentCategoryMapper.updateByPrimaryKeySelective(t);
    }

    @Override
    public PageResult selectTbContentAllByCategoryId(long categoryId) {
        PageHelper.startPage(1,5);
        TbContentExample t=new TbContentExample();
        TbContentExample.Criteria criteria = t.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> list = tbContentMapper.selectByExampleWithBLOBs(t);
        PageInfo<TbContent>pageInfo=new PageInfo<>(list);

        return new PageResult(pageInfo.getPageNum(),pageInfo.getPages(),pageInfo.getList());
    }

    @Override
    public int insertTbContent(TbContent tbContent) {
        List<AdNode>list =(List<AdNode>) redisClient.hget("大广告", tbContent.getCategoryId() + "");
        if (list.size()>0){
            redisClient.hdel("大广告",tbContent.getCategoryId());
        }
        Date date=new Date();
        tbContent.setCreated(date);
        tbContent.setUpdated(date);
        return tbContentMapper.insertSelective(tbContent);
    }

    @Override
    public int deleteContentByIds(long ids) {
        TbContent tbContent = tbContentMapper.selectByPrimaryKey(ids);
        List<AdNode>list =(List<AdNode>) redisClient.hget("大广告", tbContent.getCategoryId() + "");
        if (list.size()>0){
            redisClient.hdel("大广告",tbContent.getCategoryId()+"");
        }
        return tbContentMapper.deleteByPrimaryKey(ids);
    }

    @Override
    public List<AdNode> selectFrontendContentByAD() {
        List<AdNode> adNodeListRedis =( List<AdNode>) redisClient.hget("大广告", "89");
        if (adNodeListRedis!=null){
            return adNodeListRedis;
        }
        // 查询TbContent
        TbContentExample tbContentExample = new TbContentExample();
        TbContentExample.Criteria criteria = tbContentExample.createCriteria();
        criteria.andCategoryIdEqualTo(AD_CATEGORY_ID);
        List<TbContent> tbContentList = tbContentMapper.selectByExample(tbContentExample);
        List<AdNode> adNodeList = new ArrayList<AdNode>();
        for (TbContent tbContent : tbContentList) {
            AdNode adNode = new AdNode();
            adNode.setSrc(tbContent.getPic());
            adNode.setSrcB(tbContent.getPic2());
            adNode.setHref(tbContent.getUrl());
            adNode.setHeight(AD_HEIGHT);
            adNode.setWidth(AD_WIDTH);
            adNode.setHeightB(AD_HEIGHTB);
            adNode.setWidthB(AD_WIDTHB);
            adNodeList.add(adNode);
        }
        redisClient.hset("大广告","89",adNodeList);
        return adNodeList;
    }
}
